//discovery.js
var util = require('../../utils/util.js')
Page({
  data: {
    navTab: ["聊大咖", "Live推荐", "我购买的"],
    currentNavtab: "0",
    imgUrls: [
      'http://edu-image.nosdn.127.net/01ff2338ae3b4e8a847485f0c114ad18.png',
      'http://edu-image.nosdn.127.net/3d591af396e84b65b9f2869cecbac3b5.png',
      'http://edu-image.nosdn.127.net/882fd4ba1a0845f8af0cd5aa17644dc3.jpg'
    ],
    autoplay: true,
    interval: 5000,
    duration: 1000,
    feed: [],
    feed_length: 0,
    live:[],
    live_length:0,
    id:0
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    this.feedrefresh();
    //this.kechrefresh();
    this.getLiveList();
  },
  switchTab: function(e){
    this.setData({
      currentNavtab: e.currentTarget.dataset.idx
    });
  },

  bindItemTap: function() {
    wx.navigateTo({
      url: '../answer/answer'
    })
  },
  bindQueTap: function() {
    wx.navigateTo({
      url: '../question/question'
    })
  },

  bindLiveTap: function(e) {
    const lid = e.currentTarget.dataset.lid;
    console.log(e.currentTarget.dataset.lid);
    this.setData({
      id:e.currentTarget.dataset.lid
    } )
     wx.navigateTo({
     url: `../lesson/lesson?id=${lid}`
    })
  },

  upper: function () {
    wx.showNavigationBarLoading()
    this.refresh();
    console.log("upper");
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000);
  },
  lower: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function(){wx.hideNavigationBarLoading();that.nextLoad();}, 1000);
    console.log("lower")
  },

  refresh0: function(){
    var index_api = '';
    util.getData(index_api)
        .then(function(data){
          console.log(data);
        });
  },

  feedrefresh: function(){
    var feed = util.getDiscovery();
    console.log("loaddata");
    var feed_data = feed.data;
    this.setData({
      feed:feed_data,
      feed_length: feed_data.length
    });
  },

  //使用本地 fake 数据实现继续加载效果
  nextLoad: function(){
    var next = util.discoveryNext();
    console.log("continueload");
    var next_data = next.data;
    this.setData({
      feed: this.data.feed.concat(next_data),
      feed_length: this.data.feed_length + next_data.length
    });
  },
/*
  kechrefresh: function(){
    var kech = util.getLesson();
    console.log("loaddata");
    var kech_data = kech.data;
    this.setData({
      kech:kech_data,
      kech_length: kech_data.length
    });
  }*/


getLiveList: function() {
  var that = this;
  wx.request({
    url: 'http://1.15.55.133:8080/live/recommand',
    success(res) {
      console.log("getLiveLists: ", res);
      that.setData({//获取数据 更新列表
        live: res.data.data          
      })
    }
  })
},
});